package com.attendance.sz;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.attendance.sz.utils.ClsPublic;
import com.attendance.sz.utils.Constants;
import com.attendance.sz.utils.MyApplication;
import com.attendance.sz.utils.MyData;
import com.attendance.sz.utils.SelfDefineLoadingDialog;
import com.attendance.sz.utils.Session;
import com.attendance.sz.utils.Version;
import com.attendance.sz.utils.logMa;

import java.util.Timer;
import java.util.TimerTask;

public class SelfDefineDialog extends Activity {

	// public static AlertDialog currentAlertDialog;
	public static Dialog currentAlertDialog;
	private static final String LOGTAG = "martrin";
	private static int isTime = 15;
	public static Timer timer = null;
	public static TimerTask timerTask = null;


	public static Dialog GetCurrentDialog(Context context, View view,
			WindowManager manager, int[] layout) {
		if (currentAlertDialog != null) {
			currentAlertDialog.dismiss();
		}

		LayoutParams lp;
		if (layout == null) {
			lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
		} else {
			lp = new LinearLayout.LayoutParams(layout[0], layout[1]);
		}

		Dialog dlg = new Dialog(context);

		dlg.show();
		Window window = dlg.getWindow();

		window.setBackgroundDrawable(new ColorDrawable(0));
		window.setContentView(view, lp);

		return dlg;

	}

	public static Dialog GetCurrentAlertDialog(Context context, View view,
			WindowManager manager, int[] layout) {
		if (currentAlertDialog != null) {
			currentAlertDialog.dismiss();
		}

		LayoutParams lp;
		if (layout == null) {
			lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
		} else {
			lp = new LayoutParams(layout[0], layout[1]);
		}

		Dialog dlg = new Dialog(context, R.style.CustomDialogStyle);
		dlg.show();
		Window window = dlg.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0));
		window.setContentView(view, lp);

		return dlg;
	}

	
	
	/***
	 * 弹出卡类型选择对话框
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param view
	 */
	public static void PopupSetCardTypeDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final TextView view) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);
		
		
		
		RadioGroup mRadioGroup;
		final RadioButton cardfirst;
		final RadioButton cardsecond;
		final RadioButton cardthree;
		mRadioGroup = (RadioGroup)layoutView.findViewById(R.id.gendergroup);
		cardfirst = (RadioButton)layoutView.findViewById(R.id.cardfirst);
		cardsecond = (RadioButton)layoutView.findViewById(R.id.cardsecond);
		cardthree = (RadioButton)layoutView.findViewById(R.id.cardthree);
		
		cardfirst.setChecked(false);
		cardsecond.setChecked(false);
		cardthree.setChecked(false);
		//0x01-M1卡2.0结构，0x02-M1卡1.2结构，0x10-CPU卡1.0结构
		ClsPublic.iCardType = Integer.valueOf(Session.getCardType());
		if ( 0x01 == ClsPublic.iCardType ){
			cardfirst.setChecked(true);
		}
		else if ( 0x02 == ClsPublic.iCardType ) {
			cardsecond.setChecked(true);
		}
		else if ( 0x10 == ClsPublic.iCardType ) {
			cardthree.setChecked(true);
		}
		else {
			cardfirst.setChecked(false);
			cardsecond.setChecked(false);
			cardthree.setChecked(false);
		}

		RadioGroup.OnCheckedChangeListener radiogpchange = new RadioGroup.OnCheckedChangeListener() {
			  @Override
			  public void onCheckedChanged(RadioGroup group, int checkedId) {
			   if (checkedId == cardfirst.getId()) {
			    
			   } else if (checkedId == cardsecond.getId()) {
			    
			   } else if (checkedId == cardthree.getId()) {
				   
			   }
			   
			  }
		};
		mRadioGroup.setOnCheckedChangeListener(radiogpchange);

		

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				//0x01-M1卡2.0结构，0x02-M1卡1.2结构，0x10-CPU卡1.0结构
				if ( cardfirst.isChecked() ){
					ClsPublic.iCardType = 0x01;
					view.setText("M1卡2.0结构");
				}
				else if ( cardsecond.isChecked() ){
					ClsPublic.iCardType = 0x02;
					view.setText("M1卡1.2结构");
				}
				else if ( cardthree.isChecked() ){
					ClsPublic.iCardType = 0x10;
					view.setText("CPU卡1.0结构");
				}

				Message message = new Message();
				message.what = 0x1007+0x1000; // 2.7、读卡器参数设置（系统参数）

				Log.i("TAG","卡结构:"+ClsPublic.iCardType);
				//读卡器
				//////////////////////////////////////////////////////////////////
				//存起来
				Session.setCardType(""+ClsPublic.iCardType);
				

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}
	
	
	



	Handler dialogHandler = new Handler() {
		// 定义处理信息的方法
		public void handleMessage(Message msg) {

			switch (msg.what) {
				case 1:
					if ( isTime <= 0 ){
						CloseCurrentAlertDialog();
					}

					isTime -= 1;
					ClsPublic.ltimeCounter = 0;//计数器
					break;
			}
		}
	};

	private static Handler timerHandler = new Handler();

	/**
	 * 管理员用的对话框
	 */
	public static void PopupManagerLoginDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);
		isTime = Integer.valueOf(Session.getResidenceTime());//10;
		timerHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				CloseCurrentAlertDialog();
			}
		},isTime*1000);

		
		final EditText editPassword = (EditText) layoutView
				.findViewById(R.id.edit_password);
		if ( 0 == ClsPublic.imyDeivceID ){ ////0--表示我的手机ANDROID设备
			editPassword.setText("");
		}
		View tv = layoutView.findViewById(R.id.btnSure);

		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String password = editPassword.getText().toString().trim();
				if ( 0 == ClsPublic.imyDeivceID ){ ////0--表示我的手机ANDROID设备
					
				}
				else{
					if (password.equals("")) {
						Toast.makeText(context, "请输入密码！", Toast.LENGTH_SHORT)
								.show();
						return;
					}
	
					if ((Session.getOnePassword().equals("") && !password
							.equals(Constants.One_Passord))
							|| (!Session.getOnePassword().equals("") && !password
									.equals(Session.getOnePassword()))) {
						Toast.makeText(context, "密码错误！", Toast.LENGTH_SHORT).show();
						return;
					}
				}

				Intent intent = new Intent(context, ManagerActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

				Activity activity = (Activity) context;
				activity.startActivity(intent);

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		tv = layoutView.findViewById(R.id.tvlogin);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				ClsPublic.setbIsop(true);//设置点了
				logMa.d(LOGTAG, ":" + "tvlogin bIsop="+ClsPublic.getbIsop());
				
				CloseCurrentAlertDialog();
				
				Intent receiverIntent = new Intent(
						"com.message.remindreceiver");
				receiverIntent.putExtra("flag", 2);
				MainActivity.mainActivity.sendBroadcast(receiverIntent);
				
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	/*
	 * 管理员重新刷生产卡
	 */
	public static void PopupSetDeviceNoDialog(final Context context,
			int resourceView, final WindowManager manager, int[] layout) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// Toast.makeText(context, "请稍后，正在更新序列号！",
				// Toast.LENGTH_SHORT).show();
				CloseCurrentAlertDialog();

				Session.setCanReadCard("0");
				Session.setCanInit("1");

				
				if (MyApplication.getApp().getSimulater()){
					// 更新设备序列号
					Message message = new Message();
					message.what = 0x1006;//更新设备序列号
					// checkCardExistServiceM.this.handler.sendMessage(message);

				}
				else{
					MyData mydata = new MyData(Constants.iCmdUpdateDeviceNo, "");
					ClsPublic.dl_hfmode.myAdd(mydata);// 2.6、更新设备序列号
				}
				

				SelfDefineLoadingDialog.PopupLoadingDialog(context,
						R.layout.dialog_loading_progress, manager, null);
			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	public static void PopupChangePwdDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final String funCode) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		TextView tvTitle = (TextView) layoutView.findViewById(R.id.tvTitle);
		if (funCode.equals("1")) {
			tvTitle.setText("一级密码修改");
		} else if (funCode.equals("2")) {
			tvTitle.setText("二级密码修改");
		}

		final EditText edit_origialpassword = (EditText) layoutView
				.findViewById(R.id.edit_origialpassword);
		final EditText edit_newpassword = (EditText) layoutView
				.findViewById(R.id.edit_newpassword);
		final EditText edit_renewpassword = (EditText) layoutView
				.findViewById(R.id.edit_renewpassword);

		if (MyApplication.getApp().getSimulater()){
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 105);
			params.setMargins(0, 30, 0, 0);
			edit_origialpassword.setLayoutParams(params);
			edit_newpassword.setLayoutParams(params);
			edit_renewpassword.setLayoutParams(params);
		}
		
		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String password = edit_origialpassword.getText().toString()
						.trim();
				if (password.equals("")) {
					Toast.makeText(context, "请输入原密码！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if (funCode.equals("1")) {
					if ((Session.getOnePassword().equals("") && !password
							.equals(Constants.One_Passord))
							|| (!Session.getOnePassword().equals("") && !password
									.equals(Session.getOnePassword()))) {
						Toast.makeText(context, "原密码错误！", Toast.LENGTH_SHORT)
								.show();
						return;
					}

				} else if (funCode.equals("2")) {

					if ((Session.getSecondPassword().equals("") && !password
							.equals(Constants.Second_Passord))
							|| (!Session.getSecondPassword().equals("") && !password
									.equals(Session.getSecondPassword()))) {
						Toast.makeText(context, "原密码错误！", Toast.LENGTH_SHORT)
								.show();
						return;
					}
				}

				password = edit_newpassword.getText().toString().trim();
				if (password.equals("")) {
					Toast.makeText(context, "请输入新密码！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if (!password.equals(edit_renewpassword.getText().toString()
						.trim())) {
					Toast.makeText(context, "两次新密码输入不一致！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if (funCode.equals("1")) {
					Session.setOnePassword(password);

				} else if (funCode.equals("2")) {

					Session.setSecondPassword(password);
				}

				Toast.makeText(context, "密码修改成功！", Toast.LENGTH_SHORT).show();

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	public static void PopupCompanyInfoDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final TextView view, final String funCode) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		TextView tvTitle = (TextView) layoutView.findViewById(R.id.tvTitle);

		if (funCode.equals("1")) {
			tvTitle.setText("设置公司名称");
		}

		final EditText edit_machineno = (EditText) layoutView
				.findViewById(R.id.edit_companyinfo);

		if (funCode.equals("1")) {
			//edit_machineno.setText("欢迎使用宇川智能考勤机系统");
			edit_machineno.setText(Session.getCompanyInfo());
		} 

		View tv = layoutView.findViewById(R.id.btnSure);

		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String CompanyInfo = edit_machineno.getText().toString().trim();
				if (CompanyInfo.equals("")) {
					if (funCode.equals("1")) {
						Toast.makeText(context, "请输入公司名称！", Toast.LENGTH_SHORT)
								.show();
					} 
					return;
				}

				if (funCode.equals("1")) {
					
					Session.setCompanyInfo(CompanyInfo);
					view.setText(CompanyInfo);

				} 
				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}
	
	public static void PopupChangeMachineNoDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final TextView view, final String funCode) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		TextView tvTitle = (TextView) layoutView.findViewById(R.id.tvTitle);

		if (funCode.equals("1")) {
			tvTitle.setText("设置机器编号");
		} else if (funCode.equals("2")) {
			tvTitle.setText("设置超时时长");
		} else if (funCode.equals("3")) {
			tvTitle.setText("设置心跳间隔");	
		} else if (funCode.equals("4")) {
			tvTitle.setText("设置停留时间");
		} else if (funCode.equals("5")) {
			tvTitle.setText("设置校对时间差");
		}else if (funCode.equals("6")){
			tvTitle.setText("设置卡的密钥");
		}else if (funCode.equals("7")){
			tvTitle.setText("设置卡的扇区");
		}

		final EditText edit_machineno = (EditText) layoutView
				.findViewById(R.id.edit_machineno);

		if (funCode.equals("1")) {
			edit_machineno.setText(Version.GetMachineNo());
		} else if (funCode.equals("2")) {
			edit_machineno.setText(Version.GetOverTime());
		} else if (funCode.equals("3")) {
			edit_machineno.setText(Version.GetIntervalTime());
		} else if (funCode.equals("4")) {
			edit_machineno.setText(Session.getResidenceTime());
		} else if (funCode.equals("5")) {
			edit_machineno.setText(Session.getSynchronizationTime());
		}else if (funCode.equals("6")){
			edit_machineno.setText(Session.getCarKey());
		}else if (funCode.equals("7")){
			edit_machineno.setText(Session.getCarSection());
		}

		View tv = layoutView.findViewById(R.id.btnSure);

		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String machineNo = edit_machineno.getText().toString().trim();
				if (machineNo.equals("")) {
					if (funCode.equals("1")) {
						Toast.makeText(context, "请输入机器编号！", Toast.LENGTH_SHORT)
								.show();
					} else if (funCode.equals("2")) {
						Toast.makeText(context, "请输入超时时长！", Toast.LENGTH_SHORT)
								.show();
					} else if (funCode.equals("3")) {
						Toast.makeText(context, "请输入心跳间隔！", Toast.LENGTH_SHORT)
								.show();
					} else if (funCode.equals("4")) {
						Toast.makeText(context, "请输入停留时间！", Toast.LENGTH_SHORT)
						.show();
					} else if (funCode.equals("5")) {
						Toast.makeText(context, "请输入同步校对时间！", Toast.LENGTH_SHORT)
						.show();
					} else if (funCode.equals("6")) {
						Toast.makeText(context, "请输入卡的密钥！", Toast.LENGTH_SHORT)
								.show();
					} else if (funCode.equals("7")) {
						Toast.makeText(context, "请输入卡的扇区！", Toast.LENGTH_SHORT)
								.show();
					}

					return;
				}

				if (funCode.equals("1")) {
					if (Integer.valueOf(machineNo) <= 0
							|| Integer.valueOf(machineNo) > 9999) {
						Toast.makeText(context, "机器编号范围为1～9999！",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Session.setMachineNo(machineNo);
					view.setText(machineNo);

				} else if (funCode.equals("2")) {

					if (Integer.valueOf(machineNo) <= 0) {
						Toast.makeText(context, "超时时长必须大于0！",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Session.setOverTime(machineNo);
					view.setText(machineNo + "秒");

				} else if (funCode.equals("3")) {

					if (Integer.valueOf(machineNo) <= 0) {
						Toast.makeText(context, "心跳间隔必须大于0！",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Session.setIntervalTime(machineNo);
					view.setText(machineNo + "秒");
				} else if (funCode.equals("4")) {

					if (Integer.valueOf(machineNo) < 15) {
						Toast.makeText(context, "停留时间不能小于15！",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Session.setResidenceTime(machineNo);
					view.setText(machineNo + "秒");
				} else if (funCode.equals("5")) {

					if (Integer.valueOf(machineNo) <= 0) {
						Toast.makeText(context, "同步校对时间必须大于0！",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Session.setSynchronizationTime(machineNo);
					view.setText(machineNo + "秒");
				} else if (funCode.equals("6")) {
					Session.setCardKey(machineNo);
					view.setText(machineNo);

				} else if (funCode.equals("7")) {
					Session.setCardSection(machineNo);
					view.setText(machineNo);
				}

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	/***
	 * 弹出升级对话框
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param view
	 */
	public static void PopupSetDownAppUrlDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final TextView view) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_downurl = (EditText) layoutView
				.findViewById(R.id.edit_downurl);
		edit_downurl.setText(Version.GetDownUrl());

		CheckBox checkboxUpdate = (CheckBox)layoutView.findViewById(R.id.check_update);
		if (Session.getUpdateAuto().equals("1")) {
			checkboxUpdate.setChecked(true);
		}
		else {
			checkboxUpdate.setChecked(false);
		}
		//创建CheckBox事件监听器  
		checkboxUpdate.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			   public void onCheckedChanged(CompoundButton btn, boolean value) {
			    //value为CheckBox的值
				   if (value){
					   Session.setUpdateAuto("1");
				   }
				   else{
					   Session.setUpdateAuto("0");
				   }
			   }
		});
		

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String downurl = edit_downurl.getText().toString().trim();
				if (downurl.equals("")) {
					Toast.makeText(context, "请输入设置APP更新地址！", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				//存起来
				Session.setDownUrl(downurl);
				view.setText(downurl);
				
				//Intent receiverIntent = new Intent(
				//		"com.message.remindreceiver");
				//receiverIntent.putExtra("flag", 3);
				//MainActivity.mainActivity.sendBroadcast(receiverIntent);
				if (Session.getUpdateAuto().equals("1")) {
					
					Intent receiverIntent = new Intent("com.changedeviceno.updater");
					MainActivity.mainActivity.sendBroadcast(receiverIntent);
				}
				////////////////////////////////////////////////////////////////////
				

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	
	/***
	 * 弹出下载图片配置对话框
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param view
	 */
	public static void PopupSetDownAdvertUrlDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final TextView view) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_downurl = (EditText) layoutView
				.findViewById(R.id.edit_downurl);
		edit_downurl.setText(Session.getDownAdvUrl());

		CheckBox checkboxUpdate = (CheckBox)layoutView.findViewById(R.id.check_update);
		if (Session.getAdvAuto().equals("1")) {
			checkboxUpdate.setChecked(true);
		}
		else {
			checkboxUpdate.setChecked(false);
		}
		//创建CheckBox事件监听器  
		checkboxUpdate.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			   public void onCheckedChanged(CompoundButton btn, boolean value) {
			    //value为CheckBox的值
				   if (value){
					   Session.setAdvAuto("1");
				   }
				   else{
					   Session.setAdvAuto("0");
				   }
			   }
		});
		

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String downurl = edit_downurl.getText().toString().trim();
				if (downurl.equals("")) {
					Toast.makeText(context, "请输入设置图片服务地址！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				
				//////////////////////////////////////////////////////////////////
				//存起来
				Session.setDownAdvUrl(downurl);
				view.setText(downurl);

				

				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	/***
	 * 弹出下载图片配置对话框
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param view
	 */
	public static void PopupSetDownPhotoUrlDialog(final Context context,
												   int resourceView, WindowManager manager, int[] layout,
												   final TextView view) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_downurl = (EditText) layoutView
				.findViewById(R.id.edit_downurl);
		edit_downurl.setText(Session.getDownPhotoUrl());

		CheckBox checkboxUpdate = (CheckBox)layoutView.findViewById(R.id.check_update);
		if (Session.getAdvAuto().equals("1")) {
			checkboxUpdate.setChecked(true);
		}
		else {
			checkboxUpdate.setChecked(false);
		}
		//创建CheckBox事件监听器
		checkboxUpdate.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton btn, boolean value) {
				//value为CheckBox的值
				if (value){
					Session.setAdvAuto("1");
				}
				else{
					Session.setAdvAuto("0");
				}
			}
		});


		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String downurl = edit_downurl.getText().toString().trim();
				if (downurl.equals("")) {
					Toast.makeText(context, "请输入设置API服务地址！", Toast.LENGTH_SHORT)
							.show();
					return;
				}


				//////////////////////////////////////////////////////////////////
				//存起来
				Session.setDownPhotoUrl(downurl);
				view.setText(downurl);



				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}
	/***
	 * 弹出时间配置对话框
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param view
	 */
	public static void PopupSetDownAdverTiemlDialog(final Context context,
												   int resourceView, WindowManager manager, int[] layout,
												   final TextView view) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_downurl = (EditText) layoutView
				.findViewById(R.id.edit_downurl);
		edit_downurl.setText(Session.getDownUrlTiem());

		CheckBox checkboxUpdate = (CheckBox)layoutView.findViewById(R.id.check_update);
		if (Session.getAdvAuto().equals("1")) {
			checkboxUpdate.setChecked(true);
		}
		else {
			checkboxUpdate.setChecked(false);
		}
		//创建CheckBox事件监听器
		checkboxUpdate.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton btn, boolean value) {
				//value为CheckBox的值
				if (value){
					Session.setAdvAuto("1");
				}
				else{
					Session.setAdvAuto("0");
				}
			}
		});


		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String downurl = edit_downurl.getText().toString().trim();
				if (downurl.equals("")) {
					Toast.makeText(context, "请输入设置时间服务地址！", Toast.LENGTH_SHORT)
							.show();
					return;
				}



				//存起来
				Session.setDownUrlTiem(downurl);
				view.setText(downurl);



				CloseCurrentAlertDialog();

			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}
	/***
	 * 服务器IP地址
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param views
	 */
	public static void PopupSetServerIpDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final Object[] views) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_ip1 = (EditText) layoutView
				.findViewById(R.id.edit_ip1);
		final EditText edit_ip2 = (EditText) layoutView
				.findViewById(R.id.edit_ip2);
		final EditText edit_ip3 = (EditText) layoutView
				.findViewById(R.id.edit_ip3);
		final EditText edit_ip4 = (EditText) layoutView
				.findViewById(R.id.edit_ip4);

		if (Version.GetSeverIp().equals("0")) {
			edit_ip1.setText("0");
			edit_ip2.setText("0");
			edit_ip3.setText("0");
			edit_ip4.setText("0");
		} else {
			String[] ips = Version.GetSeverIp().split("\\.");
			edit_ip1.setText(ips[0]);
			edit_ip2.setText(ips[1]);
			edit_ip3.setText(ips[2]);
			edit_ip4.setText(ips[3]);
		}

		final EditText edit_serverport = (EditText) layoutView
				.findViewById(R.id.edit_serverport);
		edit_serverport.setText(Version.GetSeverPort());

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				logMa.d("AAA", "Start");
				if (edit_ip1.getText().toString().trim().equals("")
						|| edit_ip2.getText().toString().trim().equals("")
						|| edit_ip3.getText().toString().trim().equals("")
						|| edit_ip4.getText().toString().trim().equals("")) {
					Toast.makeText(context, "IP地址必须填写完整！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if (edit_serverport.getText().toString().trim().equals("")) {
					Toast.makeText(context, "端口号必须填写！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				String sIp = String.valueOf(Integer.valueOf(edit_ip1.getText()
						.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip2.getText()
								.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip3.getText()
								.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip4.getText()
								.toString().trim()));

				((TextView) views[0]).setText(sIp);
				((TextView) views[1]).setText(edit_serverport.getText()
						.toString().trim());

				logMa.d("AAA", "sIP="+sIp);
				Session.setServerIp(sIp);
				Session.setServerPort(edit_serverport.getText().toString()
						.trim());

				CloseCurrentAlertDialog();
				logMa.d("AAA", "sPort="+edit_serverport.getText().toString().trim());
			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}

	
	/***
	 * 服务器日志IP地址
	 * @param context
	 * @param resourceView
	 * @param manager
	 * @param layout
	 * @param views
	 */
	public static void PopupSetServerLogIpDialog(final Context context,
			int resourceView, WindowManager manager, int[] layout,
			final Object[] views) {
		View layoutView = (LinearLayout) LayoutInflater.from(context).inflate(
				resourceView, null);

		final EditText edit_ip1 = (EditText) layoutView
				.findViewById(R.id.edit_ip1);
		final EditText edit_ip2 = (EditText) layoutView
				.findViewById(R.id.edit_ip2);
		final EditText edit_ip3 = (EditText) layoutView
				.findViewById(R.id.edit_ip3);
		final EditText edit_ip4 = (EditText) layoutView
				.findViewById(R.id.edit_ip4);

		CheckBox check_Log = (CheckBox)layoutView.findViewById(R.id.check_Log);
		if (Session.getLogTranFlag().equals("1")) {
			check_Log.setChecked(true);
		}
		else {
			check_Log.setChecked(false);
		}
		//创建CheckBox事件监听器  
		check_Log.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			   public void onCheckedChanged(CompoundButton btn, boolean value) {
			    //value为CheckBox的值
				   if (value){
					   Session.setLogTranFlag("1");
				   }
				   else{
					   Session.setLogTranFlag("0");
				   }
			   }
		});
		
		if (Version.GetSeverLogIp().equals("0")) {
			edit_ip1.setText("0");
			edit_ip2.setText("0");
			edit_ip3.setText("0");
			edit_ip4.setText("0");
		} else {
			String[] ips = Version.GetSeverLogIp().split("\\.");
			edit_ip1.setText(ips[0]);
			edit_ip2.setText(ips[1]);
			edit_ip3.setText(ips[2]);
			edit_ip4.setText(ips[3]);
		}

		final EditText edit_serverport = (EditText) layoutView
				.findViewById(R.id.edit_serverport);
		edit_serverport.setText(Version.GetSeverLogPort());

		View tv = layoutView.findViewById(R.id.btnSure);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				logMa.d("AAA", "Start");
				if (edit_ip1.getText().toString().trim().equals("")
						|| edit_ip2.getText().toString().trim().equals("")
						|| edit_ip3.getText().toString().trim().equals("")
						|| edit_ip4.getText().toString().trim().equals("")) {
					Toast.makeText(context, "IP地址必须填写完整！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if (edit_serverport.getText().toString().trim().equals("")) {
					Toast.makeText(context, "端口号必须填写！", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				String sIp = String.valueOf(Integer.valueOf(edit_ip1.getText()
						.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip2.getText()
								.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip3.getText()
								.toString().trim()))
						+ "."
						+ String.valueOf(Integer.valueOf(edit_ip4.getText()
								.toString().trim()));

				((TextView) views[0]).setText(sIp);
				((TextView) views[1]).setText(edit_serverport.getText()
						.toString().trim());

				logMa.d("AAA", "sIP="+sIp);
				Session.setServerLogIp(sIp);
				Session.setServerLogPort(edit_serverport.getText().toString()
						.trim());

				/*
				 * 日志部分
				 */
				if (Session.getLogTranFlag().equals("1")){
					(MyApplication.getApp()).setDEBUGNET(true);// 通过网络输出
				}
				else{
					(MyApplication.getApp()).setDEBUGNET(false);// 不输出
				}
				(MyApplication.getApp()).setgstrDestIP(Session.getServerLogIp());
				(MyApplication.getApp()).setgiDestPort(Integer.valueOf(Session.getServerLogPort()));
				
				CloseCurrentAlertDialog();
				logMa.d("AAA", "sPort="+edit_serverport.getText().toString().trim());
			}

		});

		tv = layoutView.findViewById(R.id.btnCancel);
		tv.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CloseCurrentAlertDialog();
			}

		});

		// currentAlertDialog=GetCurrentDialog(context,layoutView,manager,layout);
		currentAlertDialog = GetCurrentAlertDialog(context, layoutView,
				manager, layout);
	}
	


	public static void CloseCurrentAlertDialog() {
		if (currentAlertDialog != null) {
			currentAlertDialog.dismiss();
		}
		
		if (timer != null) {
			timer.cancel();
			timer = null;
		}

		if (timerTask != null) {
			timerTask.cancel();
			timerTask = null;
		}

	}
}
